//Programs TABLE9A and TABLE9B produce the results in TABLE 9.
//The programs are intended to be run in tandem, with program A run first.
//It takes 5.25 hours on a laptop to run 1,000 iterations.

etime, start
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\HURWICZ BIAS\REVISION FOR ECONOMIC MODELLING\PROGRAMS2\TABLE9.smcl", replace
drop _all
clear
graph drop _all
set more off
set seed 13

local numN = 50 // This sets the number of cross-sectional units
local numT = 40 // This sets the number of time observations per unit
local beta0 = 0 // This sets the intercept term
local betax = 1 // This sets the slope coefficient for x

matrix DFEwald = J(5,5,0)
matrix DFEfieller = J(5,5,0)
matrix DGMMwald = J(5,5,0)
matrix DGMMfieller = J(5,5,0)
matrix SGMMwald = J(5,5,0)
matrix SGMMfieller = J(5,5,0)

local numN = 50 // This sets the number of cross-sectional units
local numT = 40 // This sets the number of time observations per unit
local beta0 = 0 // This sets the intercept term
local betax = 1 // This sets the slope coefficient for x

local j = 1
foreach betay in 0.60 0.70 0.80 0.90 0.95 {
local truLRP = `betax'/(1-`betay')
local i = 1
   foreach rhox in 0 0.5 0.9 0.95 1 {
   simulate pDFEwald = r(pDFEwald) pDFEfieller = r(pDFEfieller) ///
       pDGMMwald = r(pDGMMwald) pDGMMfieller = r(pDGMMfieller) ///
       pSGMMwald = r(pSGMMwald) pSGMMfieller = r(pSGMMfieller) , ///
       reps(1000): TABLE9prog, betay(`betay') truLRP(`truLRP') ///
       beta0(`beta0') betax(`betax') rhox(`rhox') numN(`numN') numT(`numT')
         
   generate RRDFEwald = 0
   replace RRDFEwald = 1 if pDFEwald < 0.05  
   summ RRDFEwald, meanonly
   matrix DFEwald[`i',`j'] = r(mean)
           
   generate RRDFEfieller = 0
   replace RRDFEfieller = 1 if pDFEfieller < 0.05  
   summ RRDFEfieller, meanonly
   matrix DFEfieller[`i',`j'] = r(mean)      
           
   generate RRDGMMwald = 0
   replace RRDGMMwald = 1 if pDGMMwald < 0.05  
   summ RRDGMMwald, meanonly
   matrix DGMMwald[`i',`j'] = r(mean)
           
   generate RRDGMMfieller = 0
   replace RRDGMMfieller = 1 if pDGMMfieller < 0.05  
   summ RRDGMMfieller, meanonly
   matrix DGMMfieller[`i',`j'] = r(mean)     
                   
   generate RRSGMMwald = 0
   replace RRSGMMwald = 1 if pSGMMwald < 0.05  
   summ RRSGMMwald, meanonly
   matrix SGMMwald[`i',`j'] = r(mean)
           
   generate RRSGMMfieller = 0
   replace RRSGMMfieller = 1 if pSGMMfieller < 0.05  
   summ RRSGMMfieller, meanonly
   matrix SGMMfieller[`i',`j'] = r(mean)     
           
   local `++i'
   }
local `++j'
} 

matrix colnames DFEwald = BY60 BY70 BY80 BY90 BY95
matrix rownames DFEfieller= R00 R50 R90 R95 R100

matrix colnames DGMMwald = BY60 BY70 BY80 BY90 BY95
matrix rownames DGMMfieller = R00 R50 R90 R95 R100

matrix colnames SGMMwald = BY60 BY70 BY80 BY90 BY95
matrix rownames SGMMfieller = R00 R50 R90 R95 R100

matrix list DFEwald
matrix list DFEfieller

matrix list DGMMwald
matrix list DGMMfieller

matrix list SGMMwald
matrix list SGMMfieller

etime

log close
